package com.yy.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yy.model.SysFileuploadEntity;
import com.yy.service.SysFileuploadService;
import com.yy.utils.PropertiesUtil;
import com.yy.utils.exception.RRException;
import com.yy.utils.ResponseEntity;
import com.yy.utils.file.FileUploadUtil;
import com.yy.utils.file.ImageUtil;
import com.yy.validator.Assert;


/**
 * 文件图片上传
 * @author yy 
 * @date 
 */
@Controller
@RequestMapping("/util/fileUpload")
public class FileUploadController {
	@Autowired
	private SysFileuploadService sysFileuploadService;
	/**
	 * 图片上传-改名-按时间储存-接口
	 * @param file 文件
	 * @param fileName 文件名 
	 * @param dirName 文件储存目录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/imageUpload")
	public ResponseEntity imageUpload(@RequestParam(value="imageFile",required=true)MultipartFile imageFile,
			@RequestParam(name="fileName",required=false)String fileName,@RequestParam(name="dirName",required=false)String dirName){
		if (imageFile.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		Assert.isBlank(dirName, "上传目录为空");
		
		//图片名
		String imageName = FileUploadUtil.reportImgName(imageFile, dirName, fileName);
		if(imageName==null){
			return  ResponseEntity.error("上传失败");
		}
		//图片远程路径
		String imgstr = String.format("%s%s%s",PropertiesUtil.getImgHttpAddress(),dirName,"/");
		String imagestr=String.format("%s%s%s",new SimpleDateFormat("yyyy/MM").format(System.currentTimeMillis()),"/",imageName);
		String imgPath = imgstr+imagestr;
		Map<String, Object> map = new HashMap<>();
		map.put("imgPath", imgPath);
		map.put("imagestr", imagestr);
		//储存信息到数据库
		SysFileuploadEntity sysFileupload =new SysFileuploadEntity();
		sysFileupload.setUrl(imgPath);
		sysFileupload.setCreateDate(new Date());
		sysFileuploadService.save(sysFileupload);
		return  ResponseEntity.ok(map);
	}
	
	/**
	 * 视频上传-改名-按时间储存-接口
	 * @param file 文件
	 * @param fileName 文件名 可不传
	 * @param dirName 文件储存目录
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/videoUpload")
	public ResponseEntity videoUpload(@RequestParam(value="imageFile",required=false)MultipartFile videoFile,
			@RequestParam(name="fileName",required=false)String fileName,@RequestParam(name="fileName",required=false)String dirName){
		if (videoFile.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		Assert.isBlank(dirName, "上传目录为空");
		//视频名
		String reportVideoName = FileUploadUtil.fileVideoUpload(videoFile, dirName, fileName);
		if(reportVideoName==null){
			return  ResponseEntity.error("上传失败");
		}
		//视频远程路径
		String videostr = String.format("%s%s%s",PropertiesUtil.getImgHttpAddress(),dirName,"/");
		String videostring=String.format("%s%s%s",new SimpleDateFormat("yyyy/MM").format(System.currentTimeMillis()),"/",reportVideoName);
		String videoPath = videostr+videostring;
		//储存信息到数据库
		SysFileuploadEntity sysFileupload =new SysFileuploadEntity();
		sysFileupload.setUrl(videoPath);
		sysFileupload.setCreateDate(new Date());
		sysFileuploadService.save(sysFileupload);
		return  ResponseEntity.response(videoPath);
	}
	/**
	 * 图片上传-按目录储存-改名-带缩略图-接口
	 * @param file 文件
	 * @param fileName 文件名 
	 * @param dirName 文件储存目录
	 * @param thumbnailMap 缩略图参数 thumbnailSize 后缀 thumbnailSizeW 宽 thumbnailSizeH 高
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/fileImageUpload")
	public ResponseEntity fileUpdateUpload(@RequestParam(value="imageFile",required=false)MultipartFile imageFile,
			@RequestParam(name="fileName",required=false)String fileName,@RequestParam(name="dirName",required=false)String dirName,
			@RequestParam(name="thumbnailMap",required=false)Map<String, Object> thumbnailMap){
		if (imageFile.isEmpty()) {
			throw new RRException("上传文件不能为空");
		}
		Assert.isBlank(dirName, "上传目录为空");
		//缩略图配置
		//String thumbnailSize = ImageUtil.thumbnailSize500_200;
		//int	thumbnailSizeW = 500;
		//int	thumbnailSizeH = 500;
		String thumbnailSize = thumbnailMap.get("thumbnailSize").toString();
		int	thumbnailSizeW = Integer.parseInt(thumbnailMap.get("thumbnailSizeW").toString());
		int	thumbnailSizeH = Integer.parseInt(thumbnailMap.get("thumbnailSizeH").toString());
		//图片名
		String imageName = FileUploadUtil.reportImgName(imageFile, dirName, fileName,thumbnailSize, thumbnailSizeW, thumbnailSizeH);
		if(imageName==null){
			return  ResponseEntity.error("上传失败");
		}
		//图片远程路径
		String imgstr = String.format("%s%s%s",PropertiesUtil.getImgHttpAddress(),dirName,"/");
		String imagestr=String.format("%s%s%s",new SimpleDateFormat("yyyy/MM").format(System.currentTimeMillis()),"/",imageName);
		String imgPath = imgstr+imagestr;
		//缩略图远程路径
		String thumbImagestr = String.format("%s%s%s%s%s",new SimpleDateFormat("yyyy/MM").format(
				System.currentTimeMillis()),"/",ImageUtil.thumbnailSuffix,thumbnailSize,imageName);
		String thumbImgPath = imgstr+thumbImagestr;
		
		Map<String, Object> map = new HashMap<>();
		map.put("imgPath", imgPath);
		map.put("thumbImgPath", thumbImgPath);
		map.put("imagestr", imagestr);
		//储存信息到数据库
		SysFileuploadEntity sysFileupload =new SysFileuploadEntity();
		sysFileupload.setUrl(imgPath);
		sysFileupload.setCreateDate(new Date());
		sysFileuploadService.save(sysFileupload);
		//储存信息到数据库
		SysFileuploadEntity sysFileuploadThumb =new SysFileuploadEntity();
		sysFileupload.setUrl(thumbImgPath);
		sysFileupload.setCreateDate(new Date());
		sysFileuploadService.save(sysFileuploadThumb);
		
		return  ResponseEntity.ok(map);
	}
	
}
